#!/bin/bash

#=======================================================================
# File ID: dd770008-300f-11df-b5fc-90e6ba3022ac
# License: GNU General Public License version 2 or later.
#=======================================================================

progname=ciall

unset opt_str
test "$1" = "-d" && {
    opt_str=-d
    test "$(git config --get ciall.noemptydirs)" = "true" && { echo $progname: -d option not allowed here >&2; exit 1; }
    shift;
    echo -n $progname: Executing git-store-dirs... >&2
    git store-dirs || {
        echo git-store-dirs error >&2;
        exit 1;
    }
    echo DONE >&2
}
besafe=n
test "$1" = "-y" && { besafe=y; opt_str=-y; shift; }
echo $progname: git status 1... >&2
LC_ALL=C git status . | grep "^nothing to commit" && { echo $progname: Nothing to do here.; exit 0; }

unset logmsg
[ ! -z "$1" ] && logmsg="$*"

echo $progname: git status 2... >&2
git status .
echo
echo "Log message: $opt_str '$logmsg'"
until test "$besafe" = "y"; do
    echo -n Press \'y\' + Enter to commit all new changes or \'n\' to abort...
    read besafe
    test "$besafe" = "n" && exit 0;
done

if [ "$logmsg" = "--" ]; then
    tmpmsgfile=/tmp/$(date +%s)-$$.tmp
    $EDITOR $tmpmsgfile
    logmsg=$(cat $tmpmsgfile)
    test -z "$logmsg" && { echo $progname: No text entered in editor, aborting.; exit 1; }
fi

echo $progname: git status 3... >&2
LC_ALL=C git status . | grep "^nothing to commit" || (echo $progname: git add --all .; git add --all . && (echo $progname: git commit; git commit -m "$logmsg`echo; echo; suuid -t $progname -c "$logmsg"`"))
echo ========== git status ==========
git status
exit 0
